TOOL.Category		= 'Render'
TOOL.Name			= '#Fireworks'
TOOL.Command		= nil
TOOL.ConfigName		= nil

if(CLIENT) then
	
	language.Add( 'Tool_fireworks_name', 'Fireworks' )
	language.Add( 'Tool_fireworks_desc', 'Create fireworks' )
	language.Add( 'Tool_fireworks_0', 'Click to spawn a firework' )

	language.Add( 'Undone_Firework', 'Undone Firework' )
	
	language.Add( 'Cleanup_fireworks', 'Fireworks' )
	language.Add( 'Cleaned_fireworks', 'Cleaned up all Fireworks' )
	language.Add( 'SBoxLimit_fireworks', 'You\'ve reached the Firework limit!' )
	
else

	CreateConVar('sbox_maxfireworks', 5)

end

cleanup.Register( 'fireworks' )

TOOL.ClientConVar[ 'r' ] = 0
TOOL.ClientConVar[ 'g' ] = 255
TOOL.ClientConVar[ 'b' ] = 0

TOOL.ClientConVar[ 'flame_r' ] = 255
TOOL.ClientConVar[ 'flame_g' ] = 165
TOOL.ClientConVar[ 'flame_b' ] = 0

TOOL.ClientConVar[ 'fusetime' ] = 2
TOOL.ClientConVar[ 'scale' ] = 1
TOOL.ClientConVar[ 'autounfreeze' ]	= 1
TOOL.ClientConVar[ 'explodetime' ]	= 0
TOOL.ClientConVar[ 'damageexplode' ]	= 0
TOOL.ClientConVar[ 'collisionexplode' ]	= 1
TOOL.ClientConVar[ 'trajectoryrand' ]	= 5
TOOL.ClientConVar[ 'blastdamage' ]	= 0


local function MakeFirework( pl, Ang, Pos, Col, FuseTime, FWScale, AutoUnfreeze, ExplodeTime, FlameColor, DamageExplode, CollisionExplode, TrajectoryRand, BlastDamage )

	if ( !pl:CheckLimit( 'fireworks' ) ) then return false end

	local Firework = ents.Create( 'sent_firework' )
	if !Firework:IsValid() then return false end
	
	Firework:SetAngles( Ang )
	Firework:SetPos( Pos )
	Firework:SetupData( Col, FuseTime, FWScale, AutoUnfreeze, ExplodeTime, FlameColor, DamageExplode, CollisionExplode, TrajectoryRand, BlastDamage )
	Firework.pl = pl
	Firework:Spawn()

	local Phys = Firework:GetPhysicsObject()
	
	if Phys:IsValid() then

		Phys:EnableMotion( false )
		
	end
	
	pl:AddCount( 'fireworks', Firework )
	pl:AddCleanup( 'fireworks', Firework )
	
	//DoPropSpawnedEffect( Firework ) // This looks terribad with the new spawn effect.

	return Firework

end

duplicator.RegisterEntityClass( 'sent_firework', MakeFirework, 'Ang', 'Pos', 'Col', 'FuseTime', 'FWScale', 'AutoUnfreeze', 'ExplodeTime', 'FlameColor', 'DamageExplode', 'CollisionExplode', 'TrajectoryRand', 'BlastDamage' )

function TOOL:LeftClick( trace )

	if ( trace.Entity && trace.Entity:IsPlayer() ) then return false end
	if (CLIENT) then return true end
	
	local ply = self:GetOwner()
	local Ang = trace.HitNormal:Angle() + Angle( 270, 0, 0 )
	
	
	local r		= self:GetClientNumber( 'r', 255 )
	local g		= self:GetClientNumber( 'g', 0 )
	local b		= self:GetClientNumber( 'b', 0 )
	
	local flame_r		= self:GetClientNumber( 'flame_r', 255 )
	local flame_g		= self:GetClientNumber( 'flame_g', 165 )
	local flame_b		= self:GetClientNumber( 'flame_b', 0 )
	
	local FuseTime			=		self:GetClientNumber( 'fusetime', 2 )
	local ExplodeTime		=		self:GetClientNumber( 'explodetime', 0 )
	local Scale				=		math.Clamp( self:GetClientNumber( 'scale', 1 ), 0.1, 3 )
	local Col				=		Color( r, g, b )
	local FlameColor		=		Vector( flame_r, flame_g, flame_b )
	local AutoUnfreeze		=		self:GetClientNumber( 'autounfreeze' ) == 1
	local DamageExplode		=		self:GetClientNumber( 'damageexplode' ) == 1
	local CollisionExplode	=		self:GetClientNumber( 'collisionexplode' ) == 1
	local TrajectoryRand	=		math.Clamp( self:GetClientNumber( 'trajectoryrand', 10 ), 0, 30 )
	local BlastDamage		=		self:GetClientNumber( 'blastdamage' ) == 1

	if ( trace.Entity:IsValid() && trace.Entity:GetClass() == 'sent_firework' && trace.Entity.pl == ply ) then

		trace.Entity:SetupData( Col, FuseTime, Scale, AutoUnfreeze, ExplodeTime, FlameColor, DamageExplode, CollisionExplode, TrajectoryRand, BlastDamage )
		
		return true
		
	end
	
	if ( !self:GetSWEP():CheckLimit( 'fireworks' ) ) then return false end
	
	local Firework = MakeFirework( ply, Ang, trace.HitPos + trace.HitNormal * 24, Col, FuseTime, Scale, AutoUnfreeze, ExplodeTime, FlameColor, DamageExplode, CollisionExplode, TrajectoryRand, BlastDamage )
	
	undo.Create( 'Firework' )
		undo.AddEntity( Firework )
		undo.SetPlayer( ply )
	undo.Finish()
	
	return true

end




function TOOL.BuildCPanel( CPanel )
	CPanel:AddControl( 'Header', { Text = '#Tool_fireworks_name', Description	= '#Tool_fireworks_desc' }  )
	
	
	// Presets
	local params = { Label = '#Presets', MenuButton = 1, Folder = 'fireworks', Options = {}, CVars = {} }
		
		params.Options.default = {
			fireworks_r					= 		'0',
			fireworks_g					=		'255',
			fireworks_b					=		'0',
			fireworks_fusetime			=		'2',
			fireworks_explodetime		=		'0',
			fireworks_trajectoryrand	=		'5',
			fireworks_scale				=		'1',		
			fireworks_autounfreeze		=		'1',
			fireworks_damageexplode		=		'0',
			fireworks_collisionexplode	=		'1',
			fireworks_blastdamage		=		'0',
			fireworks_flame_r			=		'255',
			fireworks_flame_g			=		'165',
			fireworks_flame_b			=		'0'
		}
		
		params.Options.Bomb = {
			fireworks_r					= 		'255',
			fireworks_g					=		'0',
			fireworks_b					=		'0',
			fireworks_fusetime			=		'0',
			fireworks_explodetime		=		'0.5',
			fireworks_trajectoryrand	=		'0',
			fireworks_scale				=		'2',		
			fireworks_autounfreeze		=		'0',
			fireworks_damageexplode		=		'0',
			fireworks_collisionexplode	=		'0',
			fireworks_blastdamage		=		'1',
			fireworks_flame_r			=		'255',
			fireworks_flame_g			=		'165',
			fireworks_flame_b			=		'0'
		}
		
		params.Options.Faulty = {
			fireworks_r					= 		'0',
			fireworks_g					=		'0',
			fireworks_b					=		'255',
			fireworks_fusetime			=		'2',
			fireworks_explodetime		=		'0',
			fireworks_trajectoryrand	=		'30',
			fireworks_scale				=		'0.75',		
			fireworks_autounfreeze		=		'1',
			fireworks_damageexplode		=		'1',
			fireworks_collisionexplode	=		'0',
			fireworks_blastdamage		=		'1',
			fireworks_flame_r			=		'255',
			fireworks_flame_g			=		'0',
			fireworks_flame_b			=		'0'
		}
		
		params.Options['Generic Red'] = {
			fireworks_r					= 		'255',
			fireworks_g					=		'0',
			fireworks_b					=		'0',
			fireworks_fusetime			=		'2',
			fireworks_explodetime		=		'0',
			fireworks_trajectoryrand	=		'5',
			fireworks_scale				=		'1',		
			fireworks_autounfreeze		=		'1',
			fireworks_damageexplode		=		'1',
			fireworks_collisionexplode	=		'1',
			fireworks_blastdamage		=		'1',
			fireworks_flame_r			=		'255',
			fireworks_flame_g			=		'0',
			fireworks_flame_b			=		'0'
		}
		
		params.Options['Generic Green'] = {
			fireworks_r					= 		'0',
			fireworks_g					=		'255',
			fireworks_b					=		'0',
			fireworks_fusetime			=		'2',
			fireworks_explodetime		=		'0',
			fireworks_trajectoryrand	=		'5',
			fireworks_scale				=		'1',		
			fireworks_autounfreeze		=		'1',
			fireworks_damageexplode		=		'1',
			fireworks_collisionexplode	=		'1',
			fireworks_blastdamage		=		'1',
			fireworks_flame_r			=		'0',
			fireworks_flame_g			=		'255',
			fireworks_flame_b			=		'0'
		}
		
		params.Options['Generic Blue'] = {
			fireworks_r					= 		'0',
			fireworks_g					=		'195',
			fireworks_b					=		'255',
			fireworks_fusetime			=		'2',
			fireworks_explodetime		=		'0',
			fireworks_trajectoryrand	=		'5',
			fireworks_scale				=		'1',		
			fireworks_autounfreeze		=		'1',
			fireworks_damageexplode		=		'1',
			fireworks_collisionexplode	=		'1',
			fireworks_blastdamage		=		'1',
			fireworks_flame_r			=		'0',
			fireworks_flame_g			=		'125',
			fireworks_flame_b			=		'255'
		}
		
		params.Options['Generic Purple'] = {
			fireworks_r					= 		'255',
			fireworks_g					=		'0',
			fireworks_b					=		'255',
			fireworks_fusetime			=		'2',
			fireworks_explodetime		=		'0',
			fireworks_trajectoryrand	=		'5',
			fireworks_scale				=		'1',		
			fireworks_autounfreeze		=		'1',
			fireworks_damageexplode		=		'1',
			fireworks_collisionexplode	=		'1',
			fireworks_blastdamage		=		'1',
			fireworks_flame_r			=		'255',
			fireworks_flame_g			=		'0',
			fireworks_flame_b			=		'255'
		}
		
		params.Options['Generic Yellow'] = {
			fireworks_r					= 		'255',
			fireworks_g					=		'255',
			fireworks_b					=		'0',
			fireworks_fusetime			=		'2',
			fireworks_explodetime		=		'0',
			fireworks_trajectoryrand	=		'5',
			fireworks_scale				=		'1',		
			fireworks_autounfreeze		=		'1',
			fireworks_damageexplode		=		'1',
			fireworks_collisionexplode	=		'1',
			fireworks_blastdamage		=		'1',
			fireworks_flame_r			=		'255',
			fireworks_flame_g			=		'165',
			fireworks_flame_b			=		'0'
		}
		
		table.insert( params.CVars, 'fireworks_r' )
		table.insert( params.CVars, 'fireworks_g' )
		table.insert( params.CVars, 'fireworks_b' )
		table.insert( params.CVars, 'fireworks_fusetime' )
		table.insert( params.CVars, 'fireworks_explodetime' )
		table.insert( params.CVars, 'fireworks_trajectoryrand' )
		table.insert( params.CVars, 'fireworks_scale' )
		table.insert( params.CVars, 'fireworks_autounfreeze' )
		table.insert( params.CVars, 'fireworks_damageexplode' )
		table.insert( params.CVars, 'fireworks_collisionexplode' )
		table.insert( params.CVars, 'fireworks_blastdamage' )
		table.insert( params.CVars, 'fireworks_flame_r' )
		table.insert( params.CVars, 'fireworks_flame_g' )
		table.insert( params.CVars, 'fireworks_flame_b' )
		
	CPanel:AddControl( 'ComboBox', params )
	
	CPanel:AddControl( 'Label', { Text = 'Explosion Color', Description = ''} )
	CPanel:AddControl( 'Color',  { Label	= '#Color',
									Red			= 'fireworks_r',
									Green		= 'fireworks_g',
									Blue		= 'fireworks_b',
									ShowAlpha	= 0,
									ShowHSV		= 1,
									ShowRGB 	= 1,
									Multiplier	= 255 } )	
	
	CPanel:NumSlider( 'Fuse Time', 'fireworks_fusetime', 0, 15, 2 )
	CPanel:NumSlider( 'Explode Time (0 to disable)', 'fireworks_explodetime', 0, 30, 2 )
	CPanel:NumSlider( 'Instability (0 to disable)', 'fireworks_trajectoryrand', 0, 30, 0 )	
	CPanel:NumSlider( 'Scale', 'fireworks_scale', 0.1, 3, 1 )
	
	CPanel:AddControl( 'Checkbox', { Label = 'Auto unfreeze', Command = 'fireworks_autounfreeze' } )
	CPanel:AddControl( 'Checkbox', { Label = 'Explode on damage', Command = 'fireworks_damageexplode' } )
	CPanel:AddControl( 'Checkbox', { Label = 'Explode on collision', Command = 'fireworks_collisionexplode' } )
	CPanel:AddControl( 'Checkbox', { Label = 'Blast Damage', Command = 'fireworks_blastdamage' } )
	
	CPanel:AddControl( 'Label', { Text = 'Trail Color', Description = ''} )
	CPanel:AddControl( 'Color',  { Label	= '#Color',
								Red			= 'fireworks_flame_r',
								Green		= 'fireworks_flame_g',
								Blue		= 'fireworks_flame_b',
								ShowAlpha	= 0,
								ShowHSV		= 1,
								ShowRGB 	= 1,
								Multiplier	= 255 } )	
								
	CPanel:AddControl( 'Label', { Text = 'Client Options (Affects all fireworks):', Description = 'Hey'} )
	CPanel:NumSlider( 'Flash Brightness Multiplier', 'fireworks_brightness', 0.1, 3, 2 )
	
	CPanel:AddControl( 'Checkbox', { Label = 'Show flash', Command = 'fireworks_glow' } )
	CPanel:AddControl( 'Checkbox', { Label = 'Show smoke', Command = 'fireworks_smoke' } )
	CPanel:AddControl( 'Checkbox', { Label = 'Show explosion smoke', Command = 'fireworks_smokeexplosion' } )
	CPanel:AddControl( 'Checkbox', { Label = 'Show flames', Command = 'fireworks_flame' } )
end